<?php
/**
 *  View that uses templates
 *
 */
class Yz_View_Template
    extends Yz_View_Default
{

    const NO_CONTENT = '<!-- No template -->';

    /** Template
     *  @var Yz_Template_Interface
     */
    protected $template;

    /** Layout template
     *  @var Yz_Template_Layout_Interface
     */
    protected $layout;

    /** Text (the body of view)
     *  @var string
     */
    protected $text;

    /** Location to redirect to
     *  @var string
     */
    protected $newLocation;

    /****************************
     *      Public methods      *
     ****************************/


    /** get the text (string) content of the view
     *  @return string
     */
    public function getContent()
    {
        $t =& $this->text;
        if (null === $t) {

            $content = $this->template;
            $layout = $this->layout;

            // which template to render?
            if ($layout)        // layout: render with content (which is {template|null} )
                $t = $layout->render($content);
            else if ($content)  // no layout, try to render content
                $t = $content->render();
            else
                $t = self::NO_CONTENT;
        }
        return $t;
    }

    /**
     *
     */
    public function selectLayout( $layoutName )
    {
        if ($layoutName) {
            assert('is_string($layoutName) and "" !== $layoutName');
            $class = Yz::loadClassNS( 'Template_Layout_' . $layoutName ) ;
            if (! $class)
                throw ClassError::_new_(array('className' => 'Template_Layout_' . $layoutName));
            assert('is_subclass_of($class,"Yz_Template_Layout_Interface")');
            $layout = new $class;
        } else {
            $layout = null;
        }
        $this->layout = $layout;
        return $this;
    }

    /**
     *
     */
    public function setTemplate( Yz_Template_Interface $template )
    {
        debug($template);
        $this->template = $template;
        return $this;
    }


    public function render(Yz_HTTP_Response_Interface $rs)
    {
        debug();
        if ($this->newLocation) {
            $rs->redirect($this->newLocation, $this->status); // auto subst STATUS_SEE_OTHER
            // avoid another status assignment in parent::
            $this->status = null;
            /*
                ->setStatus($rs::STATUS_SEE_OTHER)
                ->setLocation($this->newLocation)
            ;
            */
            if (! $this->template) {
                $this->template = new Yz_Template_Layout_Redirect();
                $this->template->url = $this->newLocation;
            }
        }
        return parent::render($rs);
    }


    /**
     *  @param string
     *  @param int|null $status HTTP status, default is 303 (See Other)
     */
    public function setRedirect($location, $status=null)
    {
        debug();
        assert('is_string($location) and "" !== $location');
        if (null !== $status)
            assert('is_int($status) and $status>0');
        $this->status = $status;
        $this->newLocation = $location;
        return $this;
    }

} // class
